package com.dc.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.project.entity.bo.TemplateDetailBo;
import com.dc.project.entity.map.TemplateDetailMapVo;
import com.dc.project.entity.po.TemplateDetail;
import com.dc.project.entity.query.TemplateDetailQuery;
import com.dc.project.entity.vo.TemplateDetailVo;
import com.dc.project.mapper.TemplateDetailMapper;
import com.dc.project.service.TemplateDetailService;
import com.dc.project.service.TemplateService;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * TemplateDetailServiceImpl
 *
 * @author chenxueli
 * @date 2025-03-19 13:09:50
 */
@Slf4j
@Service
public class TemplateDetailServiceImpl extends AbstractBaseService<
        TemplateDetail,
        TemplateDetailBo,
        TemplateDetailVo,
        TemplateDetailMapVo,
        TemplateDetailQuery,
        TemplateDetailMapper
        > implements TemplateDetailService {

    @Resource
    private TemplateDetailMapper templateDetailMapper;

    @Resource
    private TemplateService templateService;

    /**
     * 获取查询条件
     *
     * @param query 查询条件
     * @return 查询条件对象
     */
    @Override
    public LambdaQueryWrapper<TemplateDetail> lambdaQueryWrapper(TemplateDetailQuery query) {
        return super.lambdaQueryWrapper(query)
                .eq(StringUtils.isNotBlank(query.getTitle()), TemplateDetail::getTitle, query.getTitle())
                .eq(query.getTemplateId() != null, TemplateDetail::getFkTemplateId, query.getTemplateId())
                .eq(query.getGenerateType() != null, TemplateDetail::getGenerateType, query.getGenerateType())
                .orderByAsc(TemplateDetail::getSortOrder)
                .orderByDesc(TemplateDetail::getGmtCreate)
                .orderByDesc(TemplateDetail::getId);
    }

    /**
     * 根据模板key获取模板详情列表
     *
     * @param templateKey 模板key
     * @return 模板详情列表
     */
    @Override
    public List<TemplateDetail> listByTemplateKey(String templateKey) {
        // 查询对应的模板信息
        var template = templateService.getPoByTemplateKey(templateKey);
        if (template == null) {
            return List.of();
        }
        return listByTemplateId(template.getId());
    }

    /**
     * 根据模板id获取模板详情列表
     *
     * @param templateId 模板Id
     * @return 模板详情列表
     */
    @Override
    public List<TemplateDetail> listByTemplateId(Long templateId) {
        if (templateId == null) {
            return List.of();
        }
        var wrapper = lambdaQueryWrapper()
                .eq(TemplateDetail::getFkTemplateId, templateId)
                .orderByAsc(TemplateDetail::getSortOrder)
                .orderByDesc(TemplateDetail::getGmtCreate)
                .orderByDesc(TemplateDetail::getId);
        return templateDetailMapper.selectList(wrapper);
    }

}
